colorAdminApp.controller('processDefinitionController', function($scope,$cookieStore,$compile,$rootScope,$http,$state,$stateParams,modalService) {
	
	$scope.access_token=$cookieStore.get("access_token");
	
	$scope.easyNgPagination={
			options:{
				        currentPage:1,
				        totalCount:0,
				        pageSize:10,
			            onPageChange: function(){
			 		            	$scope.loadData();
			            }
			}
	};
	
	$scope.items=[];
	$scope.q={"name":"","key":"","category":""}
	$scope.loadData=function(){
		var currentPage=$scope.easyNgPagination.options.currentPage-1;
		var entity={"category":$scope.q.category,"key":$scope.q.key,"name":$scope.q.name,"page":currentPage,"size":$scope.easyNgPagination.options.pageSize};
        $(".dataTables_wrapper").block({ message:'正在加载数据，请稍等...',css: { top: '50%' } });
		$http.post('api/workflow/process/definition/findByPage',{params:entity}).success(function(data, status, headers, config) {
			$scope.items=data.data.content;
			$scope.easyNgPagination.options.totalCount=data.data.totalElements;
			$(".dataTables_wrapper").unblock();
		}).error(function(data, status, headers, config) {
	    	
        });  
	};
	
	
	
    $scope.showFlowChart=function(entity){
       	$scope.entity=entity;
    	$("#flowChartModal").modal("show");
	};
	
	//挂起流程定义
	$scope.suspend=function(item){
		if(!confirm("挂起后流程将无法使用，确认挂起 “" + item.name + "” 吗？")){
			return ;
		}
		$("body").block({ message:'正在执行操作，请稍等...',css: { top: '50%' } });
		$http.post('api/workflow/process/definition/suspend',{params:{"id":item.id}}).success(function(data, status, headers, config) {
			modalService.showAlertModal('注意',data.message);
			$("body").unblock();
			if(data.status==1){
				$scope.loadData(); 
			}
		}).error(function(data, status, headers, config) {
			$("body").unblock();
	    	
       });  
	};
	
	//激活流程定义
	$scope.activate=function(item){
		if(!confirm("确认激活 “" + item.name + "” 吗？")){
			return ;
		}
		$("body").block({ message:'正在执行操作，请稍等...',css: { top: '50%' } });
		$http.post('api/workflow/process/definition/activate',{params:{"id":item.id}}).success(function(data, status, headers, config) {
			modalService.showAlertModal('注意',data.message);
			$("body").unblock();
			if(data.status==1){
				$scope.loadData(); 
			}
		}).error(function(data, status, headers, config) {
			$("body").unblock();
	    	
       });  
	};
	
	//删除
	$scope.doDelete=function(item){
		if(!confirm("确认删除 “" + item.name + "” 吗？")){
			return ;
		}
		$("body").block({ message:'正在执行操作，请稍等...',css: { top: '50%' } });
		$http.post('api/workflow/process/definition/delete',{params:{"id":item.deploymentId}}).success(function(data, status, headers, config) {
			modalService.showAlertModal('注意',data.message);
			$("body").unblock();
			if(data.status==1){
				$scope.loadData(); 
			}
		}).error(function(data, status, headers, config) {
			$("body").unblock();
	    	
       });  
	};
	
    $scope.goStart=function(item){
	    	
    	$http.post('api/workflow/process/definition/getStartFormKey',{params:{"processDefinitionId":item.id}}).success(function(data, status, headers, config) {
			
    		$("body").unblock();
    		
			//formKey是流程定义里开始任务属性中设置的
			var formKey=data.data.toUpperCase();
			
			var stateCode="main."+formKey;
						
			//跳转页面，并传递流程定义ID
			$state.go(stateCode,{"processDefinitionId":item.id});
			
		}).error(function(data, status, headers, config) {
			$("body").unblock();
       });  
    	
	};
		
    
	
});
